<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-30 20:02:30
 * @LastEditTime : 2020-01-14 13:10:53
 * @LastEditors  : Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/vue-virtual-scroller.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vue-virtual-scroller.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <script type="text/javascript" src="../script/db.js"></script>
    <style type="text/css">
        .main-animate {
            height: 176px;
            overflow: hidden;
            transition: all .4s;
        }

        .toggle-1 {
            text-align: center;
            padding: 10px 0;
        }

        .toggle-1 img {
            width: 20px;
            transition: all .4s;
        }

        .toggle-1 img.rotate {
            transform: rotate(180deg);
        }

        .p-select {
            padding: 0 30px 15px;
        }

        .p-select span,
        .p-select i {
            vertical-align: middle;
        }

        .item>.van-cell__title {
            max-width: inherit;
        }

        .item .tag {
            display: inline-block;
            width: 20px;
            height: 20px;
            vertical-align: middle;
            background: #cd1b21;
            color: #fff;
            font-size: 12px;
            text-align: center;
            line-height: 20px;
            border-radius: 2px;
            position: relative;
            left: -18px;
        }

        .title {
            height: 36px;
            position: relative;
            padding-bottom: 10px;
            border-bottom: 1px dashed #cd1b21;
        }

        .title .title-text {
            float: left;
        }

        .title .title-text span {
            vertical-align: middle;
        }

        .title .title-num {
            float: right;
        }

        .title .title-num .arrow-toggle {
            display: inline-block;
            margin-left: 12px;
            width: 24px;
            text-align: center;
        }

        .title .title-num .arrow-toggle img {
            width: 16px;
            vertical-align: middle;
        }

        .title .title-num .arrow-toggle img.rotate {
            width: 16px;
            vertical-align: middle;
            transform: rotate(180deg);
        }

        .delete {
            width: 16px;
            height: 16px;
        }

        .delete img {
            width: 16px;
            vertical-align: text-top;
        }

        .txt {
            display: inline-block;
            max-width: 120px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .detail {
            height: 176px;
            overflow: hidden;
            transition: all .4s;
        }

        .detail .van-cell {
            padding: 10px 0;
        }

        .detail .van-cell:not(:last-child)::after {
            left: 0;
        }

        .hide {
            height: 0;
        }

        .shadow {
            box-shadow: 0 0 5px rgb(200, 198, 198);
            position: relative;
            background: #fff;
        }

        .font-danger .van-stepper__input {
            color: #cd1b21;
        }

        .sq {
            display: inline-block;
            width: 4px;
            height: 14px;
            background: #cd1b21;
            vertical-align: text-bottom;
            left: -15px;
            position: relative;
            box-shadow: 1px 0px 3px 0 #cd1b21;
        }

        .mode-title {
            padding: 10px 15px;
            font-size: 14px;
            font-weight: 700;
            background: #cd1b21;
            color: #fff;
        }

        .mode-type {
            line-height: 46px;
            padding: 0 15px;
            font-weight: 700;
            background: #fff;
        }

        .mode-type:active {
            background: #e6e6e6;
        }

        .err {
            width: 90%;
            overflow: hidden;
            padding: 10px;
            border-radius: 4px;
        }

        .err .t {
            line-height: 36px;
            font-size: 16px;
            border-bottom: 1px solid #dcdcdc;
        }

        .err .r {
            line-height: 36px;
            color: #969799;
        }

        .err .err-scroller {
            padding-bottom: 10px;
            height: 350px;
        }

        .err .van-cell__value {
            color: #cd1b21;
            text-align: right;
        }

        .light {
            background: rgba(255, 225, 225, 0.6);
        }

        .light .van-cell {
            background: transparent;
        }

        .pro {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            z-index: 9999;
        }

        .van-progress {
            background: #efefef;
        }

        .progress-b {
            position: absolute;
            width: 80%;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .van-progress__portion {
            height: 4px;
        }

        .scroller {
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="init-full" @click="clickDom($event)">
            <van-nav-bar class="head" title="库存盘点" left-arrow @click-left="onClickLeft"></van-nav-bar>
            <div class="body-with-bottom">
                <div class="content content-padding">
                    <a-b class="scroller" :items="formDetailData" :min-item-size="59"  key-field="'id'+'cargoID'">
                        <template #before>
                            <div style="padding: 0 10px 15px;">
                                <div class="shadow">
                                    <div class="content-label">基本信息</div>
                                    <div>
                                        <van-cell :value="formData.StockName" is-link @click="openScreen(-1,30)">
                                            <div slot="title">
                                                <span class="sq"></span>
                                                <span style="margin-left:-8px;">仓库</span>
                                            </div>
                                        </van-cell>
                                        
                                    </div>
                                    <div class="main-animate" :class="!showMain?'hide':''">
                                        <van-cell title="分支机构" :value="formData.BranchName"></van-cell>
                                        <van-cell title="经手人" :value="formData.EmplName" @click="openScreen(-1,21)"
                                            is-link>
                                        </van-cell>
                                        <van-field label="说明" v-model="formData.Explain"></van-field>
                                        <van-cell title="库存截止时间" :value="dadlineTime"></van-cell>
                                    </div>
                                    <div class="toggle-1" @click="toggleShowMain">
                                        <img src="../image/retract.png" :class="!showMain?'rotate':''" />
                                    </div>
                                </div>
                            </div>
                            <div class="p-select">
                                <van-row gutter="30">
                                    <van-col span="12">
                                        <van-button type="primary" block size="small" @click="openScan">
                                            <van-icon name="scan"></van-icon>
                                            <span>扫描二维码</span>
                                        </van-button>
                                    </van-col>
                                    <van-col span="12">
                                        <van-button type="primary" block size="small" @click="openScreen(-1,60)">
                                            <van-icon name="add-o"></van-icon>
                                            <span>选择商品</span>
                                        </van-button>
                                    </van-col>
                                </van-row>
                            </div>


                            <div>
                                <div style="padding: 0 10px;">
                                    <div class="shadow">
                                        <div class="content-label">子表明细</div>
                                        <div class="no-data" v-show="!formDetailData.length">暂无数据</div>
                                        <div>
                                        </div>
                                    </div>
                                </div>
                            </div>


                        </template>

                        <template v-slot="{ item,index,active }">
                            <b-c :item="item" :size-dependencies="[item.showDetail]" :data-index="index"
                                style="padding: 0 10px;" :active="active">
                                <van-cell class="item" :border="false" :class="item.class">
                                    <div slot="title" class="title">
                                        <div class="title-text">
                                            <span class="tag">{{index + 1}}</span>
                                            <span class="delete" @click="deleteRow(item,index)">
                                                <img src="../image/delete.png" />
                                            </span>
                                            <span class="txt">{{item.name}}</span>
                                        </div>
                                        <div class="title-num">
                                            <span>
                                                <van-stepper button-size="24" integer min="0" v-model="item.num"
                                                    @plus="changeStepper(item)" @minus="changeStepper(item)"
                                                    @blur="changeStepper(item)">
                                                </van-stepper>
                                            </span>
                                            <span class="arrow-toggle" @click="toggleShowDetail(item)">
                                                <img src="../image/retract.png" :class="!item.showDetail?'rotate':''" />
                                            </span>
                                        </div>
                                    </div>
                                    <div slot="label" class="detail" v-if="item.showDetail">
                                        <div>
                                            <van-cell title="仓库" :value="item.stockName"></van-cell>
                                            <van-cell title="库位" :value="item.cargoName" is-link
                                                @click="openScreen(index,31)"></van-cell>
                                            <van-cell title="账面库存" :value="item.stockNum">
                                            </van-cell>
                                            <van-cell title="盈亏数量" :value="item.num - item.stockNum">
                                            </van-cell>
                                        </div>
                                    </div>
                                </van-cell>
                            </b-c>
                        </template>
                    </a-b>
                </div>
                <van-loading v-if="showLoading" />

            </div>

            <div class="bottom">
                <div class="bottom-left-label">
                    <span>合计种类:</span>
                    <span class="number">{{getTotalType()}}</span>
                    <span>合计数量:</span>
                    <span class="number">{{getTotalNum()}}</span>
                </div>
                <div class="bottom-right-button">
                    <van-button block :disabled="ifConfirm" :loading="ifConfirm" loading-text="数据提交中..." type="primary"
                        @click="submitForm">
                        完成</van-button>
                </div>

            </div>
        </div>

        <van-popup v-model="showErrorList" class="err">
            <div class="t">
                本次提交<span>{{subNum}}</span>条,失败<span style="color: #cd1b21;">{{failArray.length}}</span>条
            </div>
            <div class="r">失败原因：</div>
            <a-b class="err-scroller" :items="failArray" :min-item-size="69">
                <template v-slot="{ item,index,active }">
                    <b-c :item="item" :size-dependencies="[item]" :data-index="index" :active="active">
                        <van-cell :title="item.name" :label="item.cargoName" :value="item.error"></van-cell>
                    </b-c>
                </template>
            </a-b>
            <div>
                <van-row type="flex" justify="space-around">
                    <van-col span="8">
                        <van-button block type="default" @click="showErrorList = false;">取消</van-button>
                    </van-col>
                    <van-col span="8">
                        <van-button block type="primary" @click="submitFormFail()">提交剩余</van-button>
                    </van-col>
                </van-row>
            </div>
        </van-popup>


        <div class="mask" v-if="ifConfirm">
            <div class="progress-b">
                <div class="van-progress">
                    <span class="van-progress__portion" id="progress" style="background:#1989fa;"></span>
                </div>
                <div style="color: #fff;font-size:14px;text-align:center;padding:15px;">
                    本次提交共计<span>{{subNum}}</span>条,已提交<span id="success_num">0</span>条,失败<span id="fail_num">0</span>条
                </div>
            </div>

        </div>




    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        // //$('.init-full').show();

        
        var userInfo = {};

        Vue.component('a-b', VueVirtualScroller.DynamicScroller)
        Vue.component('b-c', VueVirtualScroller.DynamicScrollerItem)

        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                showMain: false,
                ifConfirm: false,
                listLoading: false,
                listFinished: true,
                showMenu: false,
                pageIndex: 0,
                pageSize: 20,
                status: 0,
                type: 0,
                formID: 0,
                dadlineTime: FLD.getFormatDate(),
                formData: {
                    BranchID: userInfo.S_BranchID,
                    BranchName: userInfo.S_BranchName,
                    EmplID: userInfo.S_EmplID,
                    EmplName: userInfo.S_EmplName,
                    StockID: userInfo.S_StockID,
                    StockName: userInfo.S_StockName,
                    CargoID: userInfo.S_CargoID,
                    CargoName: userInfo.S_CargoName,
                    Explain: ''
                },
                formDetailData: [],
                deleteFormDetailData: [],
                failArray: [],
                subNum: 0,
                showErrorList: false
            },
            created: function () {
                var that = this;
                // that.initEvent();
                that.formDetailData = [
                    { "showDetail": false, "hasEdit": false, "id": 8459, "detailID": 83783, "name": "SA-0101", "code": "11100195", "spec": "环保", "num": 2, "stockID": 16, "stockName": "广州白云区成品仓", "cargoID": 5488, "cargoName": "广州白云区成品仓默认货位", "stockNum": 0, "unit": "套" }, 
                    { "showDetail": false, "hasEdit": false, "id": 8459, "detailID": 83783, "name": "SA-0101", "code": "11100195", "spec": "环保", "num": 5, "stockID": 163, "stockName": "广州白云区成品仓", "cargoID": 5488, "cargoName": "广州白云区成品仓默认货位", "stockNum": 0, "unit": "套" }, 
                    { "showDetail": false, "hasEdit": false, "id": 8460, "detailID": 83784, "name": "SA-0102", "code": "11100196", "spec": "环保", "num": 4, "stockID": 163, "stockName": "广州白云区成品仓", "cargoID": 5488, "cargoName": "广州白云区成品仓默认货位", "stockNum": 0, "unit": "套" }, 
                    { "showDetail": false, "hasEdit": false, "id": 8460, "detailID": 83785, "name": "SA-0103", "code": "11100197", "spec": "环保", "num": 6, "stockID": 163, "stockName": "广州白云区成品仓", "cargoID": 5488, "cargoName": "广州白云区成品仓默认货位", "stockNum": 0, "unit": "套" }
                ];

            },
            methods: {
                clickDom(e) {
                    if (!$(e.target).closest('.head-menu').length) {
                        this.showMenu = false;
                    }
                },
                initEvent() {
                    var that = this;
                    if (historyData) {
                        that.type = historyData.B_Type;
                        that.formID = historyData.BillID;
                        that.status = historyData.Status || 0;
                        that.formData = {
                            BranchID: historyData.BranchID || 0,
                            BranchName: historyData.BranchName || '',
                            EmplID: historyData.EmplID || 0,
                            EmplName: historyData.EmplName || '',
                            StockID: historyData.StockID || 0,
                            StockName: historyData.StockName || '',
                            CargoID: userInfo.S_CargoID,
                            CargoName: userInfo.S_CargoName,
                            Explain: historyData.Explain || ''
                        }
                        that.dadlineTime = historyData.BillDate;
                        that.getDetailData();

                    }
                },
                getDetailData() {
                    var that = this;
                    var obj = {
                        S_KeyID: that.formID
                    }

                    that.showLoading = true;
                    FLD.ajax('APP_PDDMXJHC', obj, function (res) {
                        that.showLoading = false;
                        if (res.ds[0].status && res.ds[0].status == 500) {
                            layer.open({
                                content: res.ds[0].msg,
                                skin: 'msg',
                                time: 2
                            });
                        } else {
                            var formDetailData = [];
                            for (var i in res.ds) {
                                formDetailData.push({
                                    showDetail: false,
                                    hasEdit: false,
                                    id: res.ds[i].ProductID,
                                    detailID: res.ds[i].StockSPDeID,
                                    name: res.ds[i].ProductName,
                                    code: res.ds[i].ProductCode,
                                    spec: res.ds[i].ProductSpec,
                                    num: res.ds[i].BNum,
                                    stockID: res.ds[i].StockID,
                                    stockName: res.ds[i].StockName,
                                    cargoID: res.ds[i].CargoID,
                                    cargoName: res.ds[i].CargoName,
                                    stockNum: res.ds[i].StockNum,
                                    unit: res.ds[i].UnitName,
                                    remark: res.ds[i].SPBZ
                                })
                            }
                            that.formDetailData = formDetailData;
                            that.listFinished = false;
                        }
                    })
                },
                changeStepper(row) {
                    var that = this;
                    row.hasEdit = true;
                },
                openScreen(type, index) {
                    if (index == 60) {
                        if (!this.formData.StockName) {
                            layer.open({
                                content: '请先选择仓库!',
                                skin: 'msg',
                                time: 2
                            });
                            return false;
                        }
                    }
                    var obj = null;
                    if (index == 30 && type > -1) {
                        obj = {
                            S_Type: index,
                            S_Cont: '',
                            S_BranchID: this.formData.BranchID,
                            S_SXZ: '',
                            S_StockID: this.formData.StockID,
                            S_CargoID: this.formData.CargoID,
                            S_BarCode: '',
                            S_PriceN: this.formData.PriceN,
                            S_ProductID: this.formDetailData[type].id
                        }
                    } else if (index == 31 && type > -1) {
                        obj = {
                            S_Type: index,
                            S_Cont: '',
                            S_BranchID: this.formData.BranchID,
                            S_SXZ: this.formDetailData[type].stockID,
                            S_StockID: this.formData.StockID,
                            S_CargoID: this.formData.CargoID,
                            S_BarCode: '',
                            S_PriceN: this.formData.PriceN,
                            S_ProductID: this.formDetailData[type].id
                        }
                    } else {
                        obj = {
                            S_Type: index,
                            S_Cont: '',
                            S_BranchID: this.formData.BranchID,
                            S_SXZ: '',
                            S_StockID: this.formData.StockID,
                            S_CargoID: this.formData.CargoID,
                            S_BarCode: '',
                            S_PriceN: this.formData.PriceN,
                            S_ProductID: 0
                        }
                    }

                    if (index == 60) {
                        api.openWin({
                            name: '筛选' + index,
                            url: 'common/commodityScreen.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                filterData: obj,
                                type: type,
                                isSimple: true
                            }
                        })
                    } else {
                        api.openWin({
                            name: '筛选' + index,
                            url: 'common/baseScreen.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                filterData: obj,
                                type: type,
                                allowClear: false
                            }
                        })
                    }

                },
                openScan() {
                    var that = this;
                    if (!that.formData.StockName) {
                        layer.open({
                            content: '请先选择仓库!',
                            skin: 'msg',
                            time: 2
                        });
                        return false;
                    }
                    FNScanner.open({
                        autorotation: true
                    }, function (ret, err) {
                        if (ret) {
                            if (ret.eventType == 'success') {
                                var obj = {
                                    S_Type: 60,
                                    S_Cont: '',
                                    S_BranchID: that.formData.BranchID,
                                    S_SXZ: '',
                                    S_StockID: that.formData.StockID,
                                    S_CargoID: that.formData.CargoID,
                                    S_BarCode: ret.content,
                                    S_PriceN: that.formData.PriceN,
                                    S_ProductID: 0
                                }
                                FLD.ajax('AP_BasList', obj, function (res) {
                                    if (res.ds.length == 0) {
                                        layer.open({
                                            content: '当前二维码未查询到商品!',
                                            skin: 'msg',
                                            time: 2
                                        });
                                        return false;
                                    }
                                    var row = res.ds[0];
                                    var x = 0;
                                    for (var i in that.formDetailData) {
                                        if (row.id == that.formDetailData[i].id && that.formData.CargoID == that.formDetailData[i].cargoID) {
                                            x = 1;
                                            var selectRow = JSON.parse(JSON.stringify(that.formDetailData[i]))
                                            that.formDetailData.splice(i, 1);
                                            that.formDetailData.unshift(selectRow);
                                            layer.open({
                                                content: '当前商品已存在,请确定后编辑数量!',
                                                skin: 'msg',
                                                time: 2
                                            });
                                        }
                                    }
                                    if (x == 0) {
                                        that.formDetailData.unshift({
                                            showDetail: false,
                                            hasEdit: false,
                                            id: row.id,
                                            detailID: 0,
                                            name: row.name,
                                            code: row.code,
                                            spec: row.Spec,
                                            num: row.StockNum,
                                            stockID: vm.formData.StockID,
                                            stockName: vm.formData.StockName,
                                            cargoID: vm.formData.CargoID,
                                            cargoName: vm.formData.CargoName,
                                            stockNum: row.StockNum,
                                            unit: row.Unit,
                                            remark: row.SPBZ
                                        })

                                    }




                                })
                            }
                        } else {
                            layer.open({
                                content: '扫描失败!',
                                skin: 'msg',
                                time: 2
                            });

                        }
                    });
                },
                deleteRow(row, index) {
                    var that = this;
                    this.$dialog.confirm({
                        title: '提示',
                        message: '是否删除该商品?'
                    }).then(() => {
                        for (var i in that.failArray) {
                            if (that.failArray[i].id == row.id && that.failArray[i].cargoID == row.cargoID) {
                                that.failArray.splice(i, 1);
                                break;
                            }

                        }

                        if (row.detailID != 0) {
                            this.deleteFormDetailData.push(row);
                        }
                        this.formDetailData.splice(index, 1);

                    }).catch(() => {

                    });
                },
                submitFormFail() {
                    var that = this;
                    that.showErrorList = false;
                    that.$nextTick(function () {
                        that.submitForm();
                    })

                },
                submitForm() {
                    var that = this;
                    that.subNum = that.formDetailData.filter(function (row) {
                        return !row.detailID || row.hasEdit;
                    }).length;
                    that.subNum = that.subNum + that.deleteFormDetailData.length;
                    that.subNum = 806;
                    var subFinishNum = 0;
                    var failArray = [];
                    $('#progress').width(0);
                    that.ifConfirm = true;
                    if (that.formID) {

                        var obj = {
                            S_BType: 2,
                            S_KeyID: that.formID,
                            S_BranchID: that.formData.BranchID,
                            S_StockID: that.formData.StockID,
                            S_EmplID: that.formData.EmplID
                        }
                        FLD.ajax('APP_PDZBAN', obj, function (res) {

                            if (res.ds[0].status == 200) {

                                var funSuccess = function () {
                                    setTimeout(function () {
                                        that.ifConfirm = false;
                                        that.deleteFormDetailData = [];
                                        that.failArray = failArray;
                                        if (that.failArray.length == 0) {
                                            layer.open({
                                                content: '保存成功!',
                                                skin: 'msg',
                                                time: 2
                                            });
                                        } else {

                                            $('.content').scrollTop(0);
                                            for (var i in that.failArray) {
                                                for (var j in that.formDetailData) {
                                                    if (that.failArray[i].id == that.formDetailData[j].id && that.failArray[i].cargoID == that.formDetailData[j].cargoID) {
                                                        var selectRow = JSON.parse(JSON.stringify(vm.formDetailData[j]));
                                                        vm.formDetailData.splice(j, 1);
                                                        vm.formDetailData.unshift(selectRow);
                                                        break;
                                                    }
                                                }
                                            }
                                            for (var i in that.formDetailData) {
                                                if (i < that.failArray.length) {
                                                    that.formDetailData[i].class = 'light';
                                                } else {
                                                    that.formDetailData[i].class = '';
                                                }
                                            }
                                            that.showErrorList = true;

                                        }
                                    }, 500)

                                }

                                var repeatDeleteDetail = function (i) {
                                    if (i == that.deleteFormDetailData.length) {
                                        funSuccess();
                                    } else {
                                        var detailObj = {
                                            S_BType: 3,
                                            S_KeyID: that.formID,
                                            S_KeyDeID: that.deleteFormDetailData[i].detailID,
                                            S_StockID: that.deleteFormDetailData[i].stockID,
                                            S_CargoID: that.deleteFormDetailData[i].cargoID,
                                            S_ProductID: that.deleteFormDetailData[i].id,
                                            S_StockNum: that.deleteFormDetailData[i].stockNum,
                                            S_BNum: that.deleteFormDetailData[i].num,
                                            S_PNum: that.deleteFormDetailData[i].num - that.deleteFormDetailData[i].stockNum
                                        }
                                        $.ajax({
                                            type: 'post',
                                            data: detailObj,
                                            url: 'http://' + FLD.serverIP + '/Frame/SqlData?cmdname=APP_PDDZBTZN' + '&platform=' + api.systemType + '【' + api.deviceId + '】',
                                            async: true,
                                            dataType: 'json',
                                            success: function (data) {
                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));
                                                if (data.status == 200) {
                                                    var res_data = data.data;
                                                    if (res_data.ds[0].status == 500) {
                                                        failArray.push({
                                                            id: that.deleteFormDetailData[i].id,
                                                            cargoID: that.deleteFormDetailData[i].cargoID,
                                                            name: that.deleteFormDetailData[i].name,
                                                            cargoName: that.deleteFormDetailData[i].cargoName,
                                                            error: res_data.ds[0].msg
                                                        })
                                                        $('#fail_num').html(failArray.length);
                                                    }
                                                } else {
                                                    failArray.push({
                                                        id: that.deleteFormDetailData[i].id,
                                                        cargoID: that.deleteFormDetailData[i].cargoID,
                                                        name: that.deleteFormDetailData[i].name,
                                                        cargoName: that.deleteFormDetailData[i].cargoName,
                                                        error: '网络异常'
                                                    })
                                                    $('#fail_num').html(failArray.length);
                                                }
                                                i++;
                                                repeatDeleteDetail(i);
                                            },
                                            error: function (err) {
                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));
                                                failArray.push({
                                                    id: that.deleteFormDetailData[i].id,
                                                    cargoID: that.deleteFormDetailData[i].cargoID,
                                                    name: that.deleteFormDetailData[i].name,
                                                    cargoName: that.deleteFormDetailData[i].cargoName,
                                                    error: '网络异常'
                                                })
                                                $('#fail_num').html(failArray.length);
                                                i++;
                                                repeatDeleteDetail(i);
                                            }
                                        });
                                    }
                                }



                                var repeatDetail = function (i) {
                                    if (i == that.formDetailData.length) {
                                        if (that.deleteFormDetailData.length) {
                                            repeatDeleteDetail(0);
                                        } else {
                                            funSuccess();
                                        }

                                    } else {
                                        var subType = 2;
                                        // if (that.formDetailData[i].detailID == 0) {
                                        //     subType = 1;
                                        // } else if (that.formDetailData[i].hasEdit) {
                                        //     subType = 2;
                                        // } else {
                                        //     i++;
                                        //     repeatDetail(i);
                                        // }
                                        var detailObj = {
                                            S_BType: subType,
                                            S_KeyID: that.formID,
                                            S_KeyDeID: that.formDetailData[i].detailID,
                                            S_StockID: that.formDetailData[i].stockID,
                                            S_CargoID: that.formDetailData[i].cargoID,
                                            S_ProductID: that.formDetailData[i].id,
                                            S_StockNum: that.formDetailData[i].stockNum,
                                            S_BNum: that.formDetailData[i].num,
                                            S_PNum: that.formDetailData[i].num - that.formDetailData[i].stockNum
                                        }

                                        $.ajax({
                                            type: 'post',
                                            data: detailObj,
                                            url: 'http://' + FLD.serverIP + '/Frame/SqlData?cmdname=APP_PDDZBTZN' + '&platform=' + api.systemType + '【' + api.deviceId + '】',
                                            async: true,
                                            dataType: 'json',
                                            success: function (data) {
                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));

                                                if (data.status == 200) {
                                                    var res_data = data.data;
                                                    if (res_data.ds[0].status == 500) {
                                                        failArray.push({
                                                            id: that.formDetailData[i].id,
                                                            cargoID: that.formDetailData[i].cargoID,
                                                            name: that.formDetailData[i].name,
                                                            cargoName: that.formDetailData[i].cargoName,
                                                            error: res_data.ds[0].msg
                                                        })
                                                        $('#fail_num').html(failArray.length);
                                                    } else {
                                                        if (subType == 1) {
                                                            that.formDetailData[i].detailID = res_data.ds[0].S_BillDeID;
                                                        }
                                                        that.formDetailData[i].hasEdit = false;
                                                    }
                                                } else {
                                                    failArray.push({
                                                        id: that.formDetailData[i].id,
                                                        cargoID: that.formDetailData[i].cargoID,
                                                        name: that.formDetailData[i].name,
                                                        cargoName: that.formDetailData[i].cargoName,
                                                        error: '网络异常'
                                                    })
                                                    $('#fail_num').html(failArray.length);
                                                }
                                                i++;
                                                repeatDetail(i);
                                            },
                                            error: function (err) {

                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));
                                                failArray.push({
                                                    id: that.formDetailData[i].id,
                                                    cargoID: that.formDetailData[i].cargoID,
                                                    name: that.formDetailData[i].name,
                                                    cargoName: that.formDetailData[i].cargoName,
                                                    error: '网络异常'
                                                })
                                                $('#fail_num').html(failArray.length);
                                                i++;
                                                repeatDetail(i);
                                            }
                                        });
                                    }
                                }
                                if (that.subNum == 0) {
                                    funSuccess();
                                } else {
                                    if (that.formDetailData.length) {
                                        repeatDetail(0);
                                    } else {
                                        funSuccess();
                                    }
                                }



                            } else {
                                that.ifConfirm = false;
                                layer.open({
                                    content: res.ds[0].msg,
                                    skin: 'msg',
                                    time: 2
                                });
                            }



                        })



                    } else {
                        var obj = {
                            S_BType: 1,
                            S_KeyID: 0,
                            S_BranchID: that.formData.BranchID,
                            S_StockID: that.formData.StockID,
                            S_EmplID: that.formData.EmplID
                        }
                        FLD.ajax('APP_PDZBAN', obj, function (res) {
                            if (res.ds[0].status == 200) {
                                that.formID = res.ds[0].S_BillID;

                                var funSuccess = function () {
                                    setTimeout(function () {
                                        that.ifConfirm = false;
                                        that.failArray = failArray;
                                        if (that.failArray.length == 0) {
                                            layer.open({
                                                content: '保存成功!',
                                                skin: 'msg',
                                                time: 2
                                            });
                                        } else {
                                            $('.content').scrollTop(0);
                                            for (var i in that.failArray) {
                                                for (var j in that.formDetailData) {
                                                    if (that.failArray[i].id == that.formDetailData[j].id && that.failArray[i].cargoID == that.formDetailData[j].cargoID) {
                                                        var selectRow = JSON.parse(JSON.stringify(vm.formDetailData[j]))
                                                        vm.formDetailData.splice(j, 1);
                                                        vm.formDetailData.unshift(selectRow);
                                                        break;
                                                    }
                                                }
                                            }
                                            for (var i in that.formDetailData) {
                                                if (i < that.failArray.length) {
                                                    that.formDetailData[i].class = 'light';
                                                } else {
                                                    that.formDetailData[i].class = '';
                                                }
                                            }
                                            that.showErrorList = true;
                                        }
                                    }, 200)

                                }

                                var repeatDetail = function (i) {
                                    if (i == that.formDetailData.length) {
                                        if (that.deleteFormDetailData.length) {
                                            repeatDeleteDetail(0);
                                        } else {
                                            funSuccess();
                                        }

                                    } else {
                                        var detailObj = {
                                            S_BType: 1,
                                            S_KeyID: that.formID,
                                            S_KeyDeID: 0,
                                            S_StockID: that.formDetailData[i].stockID,
                                            S_CargoID: that.formDetailData[i].cargoID,
                                            S_ProductID: that.formDetailData[i].id,
                                            S_StockNum: that.formDetailData[i].stockNum,
                                            S_BNum: that.formDetailData[i].num,
                                            S_PNum: that.formDetailData[i].num - that.formDetailData[i].stockNum
                                        }
                                        $.ajax({
                                            type: 'post',
                                            data: detailObj,
                                            url: 'http://' + FLD.serverIP + '/Frame/SqlData?cmdname=APP_PDDZBTZN' + '&platform=' + api.systemType + '【' + api.deviceId + '】',
                                            async: true,
                                            dataType: 'json',
                                            success: function (data) {
                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));
                                                if (data.status == 200) {
                                                    var res_data = data.data;
                                                    if (res_data.ds[0].status == 500) {
                                                        failArray.push({
                                                            id: that.formDetailData[i].id,
                                                            cargoID: that.formDetailData[i].cargoID,
                                                            name: that.formDetailData[i].name,
                                                            cargoName: that.formDetailData[i].cargoName,
                                                            error: res_data.ds[0].msg
                                                        })
                                                        $('#fail_num').html(failArray.length);
                                                    } else {
                                                        that.formDetailData[i].detailID = res_data.ds[0].S_BillDeID;
                                                        that.formDetailData[i].hasEdit = false;
                                                    }
                                                } else {
                                                    failArray.push({
                                                        id: that.formDetailData[i].id,
                                                        cargoID: that.formDetailData[i].cargoID,
                                                        name: that.formDetailData[i].name,
                                                        cargoName: that.formDetailData[i].cargoName,
                                                        error: '网络异常'
                                                    })
                                                    $('#fail_num').html(failArray.length);
                                                }
                                                i++;
                                                repeatDetail(i);
                                            },
                                            error: function (err) {
                                                subFinishNum++;
                                                $('#success_num').html(subFinishNum);
                                                $('#progress').width((subFinishNum / that.subNum * 100 + '%'));
                                                failArray.push({
                                                    id: that.formDetailData[i].id,
                                                    cargoID: that.formDetailData[i].cargoID,
                                                    name: that.formDetailData[i].name,
                                                    cargoName: that.formDetailData[i].cargoName,
                                                    error: '网络异常'
                                                })
                                                $('#fail_num').html(failArray.length);
                                                i++;
                                                repeatDetail(i);
                                            }
                                        });
                                    }
                                }
                                if (that.formDetailData.length) {
                                    repeatDetail(0);
                                } else {
                                    funSuccess();
                                }




                            } else {
                                that.ifConfirm = false;
                                layer.open({
                                    content: res.ds[0].msg,
                                    skin: 'msg',
                                    time: 2
                                });
                            }
                        })
                    }
                },
                toggleShowMain() {
                    this.showMain = !this.showMain;
                },
                toggleShowDetail(row) {
                    var that = this;
                    row.showDetail = !row.showDetail;
                },
                getTotalType() {
                    var that = this;
                    var newArray = that.formDetailData;
                    var newArray_noRepeat = []
                    for (var i in newArray) {
                        var x = 0;
                        for (var j in newArray_noRepeat) {
                            if (newArray[i].id == newArray_noRepeat[j].id && newArray[i].cargoID == newArray_noRepeat[j].cargoID) {
                                x = 1;
                            }
                        }
                        if (x == 0) {
                            newArray_noRepeat.push(newArray[i])
                        }
                    }
                    return newArray_noRepeat.length;
                },
                getTotalNum() {
                    var that = this;
                    var newArray = that.formDetailData;
                    var total = 0;
                    var newArray_noRepeat = [];
                    for (var i in newArray) {
                        var x = 0;
                        for (var j in newArray_noRepeat) {
                            if (newArray[i].id == newArray_noRepeat[j].id && newArray[i].cargoID == newArray_noRepeat[j].cargoID) {
                                x = 1;
                            }
                        }
                        if (x == 0) {
                            total += newArray[i].num;
                            newArray_noRepeat.push(newArray[i])
                        }
                    }
                    return total;
                },
                onClickLeft() {
                    if (this.type == 3) {
                        api.closeWin();
                    } else {
                        this.$dialog.confirm({
                            title: '提示',
                            message: '是否退出库存盘点界面?'
                        }).then(() => {
                            api.closeWin();
                        }).catch(() => {

                        });
                    }

                }
            }
        })
    }();
</script>

</html>